//
//  YZMThreeQutoesCell.m
//  YZMicroStock
//
//  Created by apple on 2017/11/28.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMThreeQutoesCell.h"
#import "YZMTrendView.h"
#import "YZMStockInfoView.h"
#import "YZMStockHandicapView.h"
#import "YZMStockRealModel.h"
#import "NSNumber+Utils.h"
#import "YZMAppDelegate.h"

@interface YZMThreeQutoesCell ()

@property (weak, nonatomic) IBOutlet YZMStockInfoView *infoView;

@property (weak, nonatomic) IBOutlet YZMTrendView *trendView;

@property (weak, nonatomic) IBOutlet YZMStockHandicapView *handicapView;

@property (weak, nonatomic) IBOutlet UILabel *labelName;

@property (weak, nonatomic) IBOutlet UILabel *labelCode;

@property (weak, nonatomic) IBOutlet UILabel *labelQutoes;

@property (weak, nonatomic) IBOutlet UILabel *labelProfitl;

@property (weak, nonatomic) IBOutlet UILabel *labelRange;

@property (weak, nonatomic) IBOutlet UIView *viewLeft;

@property (weak, nonatomic) IBOutlet UIView *viewRight;

@property (weak, nonatomic) IBOutlet YZMTrendView *trendViewLandscape;

@end

@implementation YZMThreeQutoesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [YZMAppStyle colorForMain];
    self.trendView.noLeftSpace = YES;
    self.handicapView.noDailylimitOrDown = YES;
    self.labelName.textColor = [YZMAppStyle colorForWhite];
    self.labelCode.textColor = [YZMAppStyle colorForLightGrayText];
    self.viewLeft.backgroundColor = [YZMAppStyle colorForMain];
    self.viewRight.backgroundColor = [YZMAppStyle colorForMain];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setTrendRotation:)];
    tap.numberOfTapsRequired = 2;
    [self.trendView addGestureRecognizer:tap];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)setRealInfo:(YZMStockRealModel *)realInfo{
        self.labelName.text = realInfo.name;
        self.labelCode.text = realInfo.code;
        if (realInfo.name.length == 0) {
            self.labelName.text = @"--";
        }
        if (realInfo.code.length == 0) {
            self.labelCode.text = @"--";
        }
        self.labelQutoes.text = @(realInfo.price).p02fString;
        double distance = realInfo.price - realInfo.lastClosePrice;
        if ([@(distance) isEqualWithDouble:0.0]) {
            self.labelQutoes.textColor = [YZMAppStyle colorForGrayText];
            self.labelProfitl.textColor = [YZMAppStyle colorForGrayText];
            self.labelRange.textColor = [YZMAppStyle colorForGrayText];
            self.labelProfitl.text = @"+0.00";
            self.labelRange.text = @"+0.00%";
            
        }else if([@(distance) isGreaterThanDouble:0.0]){
            
            self.labelQutoes.textColor = [YZMAppStyle colorForRiseText];
            self.labelProfitl.textColor = [YZMAppStyle colorForRiseText];
            self.labelRange.textColor = [YZMAppStyle colorForRiseText];
            self.labelProfitl.text = [NSString stringWithFormat:@"+%@",@(distance).p02fString];
            self.labelRange.text = [NSString stringWithFormat:@"+%@%%",@(distance*100/realInfo.lastClosePrice).p02fString];
            
        }else{
            self.labelQutoes.textColor = [YZMAppStyle colorForDropText];
            self.labelProfitl.textColor = [YZMAppStyle colorForDropText];
            self.labelRange.textColor = [YZMAppStyle colorForDropText];
            self.labelProfitl.text = [NSString stringWithFormat:@"%@",@(distance).p02fString];
            self.labelRange.text = [NSString stringWithFormat:@"%@%%",@(distance*100/realInfo.lastClosePrice).p02fString];
        }
        if ([@(realInfo.price)isEqualWithDouble:0.0]) {
            self.labelQutoes.textColor = [YZMAppStyle colorForRiseText];
            self.labelProfitl.textColor = [YZMAppStyle colorForRiseText];
            self.labelRange.textColor = [YZMAppStyle colorForRiseText];
            self.labelProfitl.text = @"0%";
            self.labelRange.text = @"0%";
        }
        self.handicapView.realInfo = realInfo;
}

- (void)setTrendPacket:(YZMTrendPacket *)trendPacket{
    [self.trendView updateFromDataPacket:trendPacket];
    [self.trendViewLandscape updateFromDataPacket:trendPacket];
}

- (void)setTrendRotation:(UITapGestureRecognizer *)tap{
    YZMAppDelegate * appDelegate = (YZMAppDelegate *)[UIApplication sharedApplication].delegate;
    
    appDelegate.allowRotation = YES;
    [[UINavigationBar appearance] setHidden:YES];
        //不设置方向
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        //强制向右横屏
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    [self.trendViewLandscape  setNeedsDisplay];
//    }else{
//        //不设置方向
//        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
//        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
//        //强制竖屏
//        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
//        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
//    }
}


@end
